package com._520it.wms.web.controller;

import com._520it.wms.domain.StockOutcomeBill;
import com._520it.wms.query.StockOutcomeBillQueryObject;
import com._520it.wms.service.IClientService;
import com._520it.wms.service.IDepotService;
import com._520it.wms.service.IStockOutcomeBillService;
import com._520it.wms.util.JSONResult;
import com._520it.wms.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by HASEE on 2017/11/14.
 */
@Controller
@RequestMapping("/stockOutcomeBill")
public class StockOutcomeBillContorller {
    @Autowired
    private IStockOutcomeBillService stockOutcomeBillService;
    @Autowired //仓库
    private IDepotService depotService;
    @Autowired//客户
    private IClientService clientService;

    @RequestMapping("/list")
    @RequiredPermission("销售出库订单列表")
    public String list(Model model, @ModelAttribute("qo") StockOutcomeBillQueryObject qo) {
        model.addAttribute("depots", depotService.listAll());
        model.addAttribute("clients" ,clientService.listAll());
        //int a = 1/0; 异常解析器
        model.addAttribute("pageResult", stockOutcomeBillService.query(qo));
        System.out.println(stockOutcomeBillService.query(qo).getList());
        return "/stockOutcomeBill/list";
    }

    @RequestMapping("/input")
    @RequiredPermission("销售出库订单编辑")
    public String input(Long id, Model model) {
        model.addAttribute("depots",depotService.listAll());
        model.addAttribute("clients", clientService.listAll());
        if (id != null) {
            StockOutcomeBill stockOutcomeBill = stockOutcomeBillService.get(id);
            model.addAttribute("stockOutcomeBill", stockOutcomeBill);
        }
        return "/stockOutcomeBill/input";
    }

    @RequestMapping("/view")
    @RequiredPermission("销售出库订单查看")
    public String view(Long id, Model model) {
        if (id != null) {
            StockOutcomeBill stockOutcomeBill = stockOutcomeBillService.get(id);
            model.addAttribute("stockOutcomeBill", stockOutcomeBill);
        }
        return "/stockOutcomeBill/views";
    }

    @RequestMapping("delete")
    @RequiredPermission("销售出库订单删除")
    @ResponseBody
    public JSONResult delete(Long id) {
        if (id != null) {
            stockOutcomeBillService.delete(id);
        }
        return new JSONResult();
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission("销售出库订单新增/更新")
    @ResponseBody
    public JSONResult saveOrUpdate(StockOutcomeBill stockOutcomeBill) {
        stockOutcomeBillService.saveOrUpdate(stockOutcomeBill);
        return new JSONResult();
    }

    @RequestMapping("/audit")
    @RequiredPermission("销售出库订单审核")
    @ResponseBody
    public JSONResult audit(Long id) {
        JSONResult jsonResult = new JSONResult();
        try {
            if (id != null) {
                stockOutcomeBillService.audit(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

}
